package com.atguigu.gmall.product.service.impl;
import com.atguigu.gmall.product.entity.SpuImage;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.entity.SpuSaleAttrValue;
import com.google.common.collect.Lists;

import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.service.SpuSaleAttrValueService;
import com.atguigu.gmall.product.vo.SpuInfoVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.product.entity.SpuInfo;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.mapper.SpuInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* @author ysy
* @description 针对表【spu_info(商品表)】的数据库操作Service实现
* @createDate 2023-10-07 23:08:39
*/
@Service
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo>
    implements SpuInfoService{

    @Autowired
    SpuInfoService spuInfoService;
    @Autowired
    SpuImageService spuImageService;
    @Autowired
    SpuSaleAttrService spuSaleAttrService;
    @Autowired
    SpuSaleAttrValueService spuSaleAttrValueService;

    @Transactional
    @Override
    public void saveSpuInfo(SpuInfoVo spuInfoVo) {
        //1,基本信息保存到spu_info
        SpuInfo info = new SpuInfo();
        BeanUtils.copyProperties(spuInfoVo,info);
        spuInfoService.save(info);
        Long spuId = info.getId();

        //2,图片信息保存到spu_image
        List<SpuImage> spuImages = new ArrayList<>();
        List<SpuInfoVo.SpuImageListDTO> spuImageList = spuInfoVo.getSpuImageList();
        for (SpuInfoVo.SpuImageListDTO image : spuImageList) {
            SpuImage spuImage = new SpuImage();
            BeanUtils.copyProperties(image,spuImageList);
            //回填SPU_ID
            spuImage.setSpuId(spuId);
            spuImages.add(spuImage);
        }
        spuImageService.saveBatch(spuImages);

        //3,销售属性名保存到spu_sale_attr
        List<SpuSaleAttr> attrs = new ArrayList<>();
        List<SpuSaleAttrValue> attrValues = new ArrayList<>();

        List<SpuInfoVo.SpuSaleAttrListDTO> attrList = spuInfoVo.getSpuSaleAttrList();
        for (SpuInfoVo.SpuSaleAttrListDTO attr : attrList) {
            //3.1准备销售名信息
            SpuSaleAttr spuSaleAttr = new SpuSaleAttr();
            BeanUtils.copyProperties(attr,spuSaleAttr);
            //回填SPU_ID
            spuSaleAttr.setSpuId(spuId);
            attrs.add(spuSaleAttr);

            //3.2准备销售值信息
            for (SpuInfoVo.SpuSaleAttrListDTO.SpuSaleAttrValueListDTO spuSaleAttrValueListDTO : attr.getSpuSaleAttrValueList()) {
                SpuSaleAttrValue saleAttrValue = new SpuSaleAttrValue();

                BeanUtils.copyProperties(spuSaleAttrValueListDTO,saleAttrValue);
                //回填参数
                saleAttrValue.setSpuId(spuId);
                saleAttrValue.setSaleAttrName(attr.getSaleAttrName());

                attrValues.add(saleAttrValue);
            }
        }
        spuSaleAttrService.saveBatch(attrs);

        //4,销售属性值保存到spu_sale_attr_value
        spuSaleAttrValueService.saveBatch(attrValues);
    }
}




